// pages/nutrition/nutrition.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: "",
    xiangqing: [],
    tupian: '',
    tilte: '',
    reliang: '',
    sercherStorage: [],
    StorageFlag: false,
    aijilu: {},
    componentData: [{
        color: '#7DE3EF',
        name: '蛋白质',
        g: 0,
        percent: '35%',
      },
      {
        color: '#1E85FB',
        name: '脂肪',
        g: 0,
        percent: '42%',
      },
      {
        color: '#AF8FFD',
        name: '碳水',
        g: 0,
        percent: '20%'
      },
    ],
    recDishData: [{
      des: '做法步骤:第1步、苹果洗净切小块。第2步、加入食用油,适量白糖。第3步、小火翻炒,淋淀粉',
      star: 3.5
    }, 1, 1, 1],
    recSPData: [],
    alikeFoods: [],
    tabooFoods: [],
    degreeData: ['相对较低', '相对一般', '相对较高'],
    effectData: '',
    tabooData: '',
    percent: ''
  },
  getFullStar: function (e) {

    console.log(e.currentTarget.dataset.i);
    return 3
  },
  getTabooData: function (name) {
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/scanreversefood?name=' + name,
      success: res => {
        console.log(res.data.data.data);
        var d = res.data.data.data
        for (const key in d) {
          wx.request({
            url: 'https://www.daizhongdai.xyz:3007/getserch?name=' + d[key].name,
            success: res => {
              console.log(res.data.data);
              if (res.data.data.length != 0) {
                d[key].img = res.data.data[0].smallImgUrl
                var dd = this.data.tabooFoods
                dd.push(d[key])
                this.setData({
                  tabooFoods: dd
                })
                console.log(this.data.tabooFoods);
              }


            }
          })
        }

      }
    })
  },
  getEffect:function(name,isEffec=true){
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/robot?query='+name+(isEffec?'的功效？':'的禁忌？')+'&id='+(isEffec?'161199':'101291'),
      success:res=>{
        var d = res.data.data
        console.log(d);
        if(d.error_code!=0 || d.result.response.action_list[0].type=="failure"){
          // wx.showToast({
          //   title: '出错了...',
          //   icon:'error'
          // })
          return
        }
if(isEffec){
  this.setData({
    effectData:d.result.response.action_list[0].say
  });
}else{
  this.setData({
    tabooData:d.result.response.action_list[0].say
  });
}
    
 
      }
    })
  },
  jumpCaiPu: function (e) {
    //e.currentTarget.dataset.text获取点击的模块的值
    console.log(e);
    wx.navigateTo({
      url: '../dishDetails/dishDetails?info=' + JSON.stringify(e.currentTarget.dataset.index),
    })
    console.log(e.currentTarget.dataset.index);
  },
  getDetail(name) {
    wx.showLoading({
      title: '正在加载中！',
    })
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/getDishDetail?name=' + name,
      success: res => {
        console.log(res.data.data.data[0]);
        var d = this.data.componentData
        d[0].g = Number(res.data.data.data[0]['蛋白质（克）']).toFixed(2)
        d[1].g = Number(res.data.data.data[0]['脂肪（克）']).toFixed(2)
        d[2].g = Number(res.data.data.data[0]['碳水化物（克）']).toFixed(2)
        d[0].percent = this.Percentage(d[0].g, 100)
        d[1].percent = this.Percentage(d[1].g, 100)
        d[2].percent = this.Percentage(d[2].g, 100)
        this.setData({
          detail: res.data.data.data[0],
          componentData: d,
          percent: Number(d[1].percent.substring(0, d[1].percent.length-1))

        })
        // console.log(d[1].percent.substring(0, d[1].percent.length-1));
      },
      complete: res => {
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  Percentage: function (number1) {
    return number1 * 100 / 100 + "%"
  },
  getGeuessYourLike: function (name) {
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/like/query?name=' + name,
      success: (res) => {
        console.log(res.data.data.data);
        this.setData({
          caidan: res.data.data.data,
        })
      },
      fail: () => {

      }
    })
    console.log(name);
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/getserch?name=' + name,
      success: (res) => {
        console.log(res.data.data[0]);
        if(res.data.data.length==0){
          this.getGeuessYourLike('苹果')
          return
        }
        this.setData({
          xiangqing: res.data.data[0],
          tilte: res.data.data[0].name,
          tupian: res.data.data[0].smallImgUrl,
          aijilu: res.data.data[0]
        })
        wx.request({
          url: 'https://www.daizhongdai.xyz:3007/insert/scann?avator=' + this.data.tupian + '&' + 'title=' + this.data.tilte + '&' + 'brief=' + this.data.aijilu.measurement,
          method: 'GET',
          header: {
            'content-type': 'application/json', // 默认值
            'sessiontoken': wx.getStorageSync('token')
          },
          success: (res) => {
            console.log(res);
            console.log('插入记录成功');
          }
        })

      }
    })


  },

  getRecSP: function () {
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/index/tj',
      success: res => {
        console.log(res.data.data.data);
        this.setData({
          recSPData: res.data.data.data
        })
      }
    })
  },
  goToDetail: function (e) {
    console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    var dd = this.data.recSPData
    var d = {
      title: dd[i].title,
      thumb: dd[i].thumb,
      fl: dd[i].fl,
      yl: dd[i].yl,
      steptext: dd[i].steptext,
    }
    wx.navigateTo({
      url: '../dishDetails/dishDetails?info=' + JSON.stringify(d),
    })
  },
  aiThis: function (e) {
    console.log(e.currentTarget.dataset.d);
    var dd = e.currentTarget.dataset.d
    this.onLoad({
      name: JSON.stringify(dd)
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      tabooFoods:[],
      alikeFoods:[]
    })
    console.log(options);
    this.getRecSP()
    var n = JSON.parse(options.name)
    console.log(n + "6666");
    this.getGeuessYourLike(n[0].name || n)

    this.getDetail(n[0].name || n)
    this.getTabooData(n[0].name || n)
    this.getEffect(n[0].name || n,true)
    // this.getForbid(n[0].name || n)
    if (!n[0].name)
      return
    for (var i = 1; i < n.length; i++) {
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/getserch?name=' + n[i].name,
        success: res => {
          console.log(res.data.data);
          if (res.data.data.length != 0) {
            var d = {
              name: res.data.data[0].name,
              img: res.data.data[0].smallImgUrl
            }
            var alikeFoods = this.data.alikeFoods
            alikeFoods.push(d)
            this.setData({
              alikeFoods
            })
          }
        }
      })
    }


 setTimeout(()=>{
  //sqs限制
  this.getEffect(n[0].name || n,false)
 },3000)


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})